package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.Md5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.service.IEmployeeService;
import cn.itsource.user.domain.Logininfo;
import cn.itsource.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：员工
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public void add(Employee employee) {
        //生成盐值，再加密加盐
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //先添加logininfo表
        Logininfo logininfo = new Logininfo();
        //根据两张表相同的字段匹配，然后复制到另一张表中
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0); //设置为管理员
        logininfo.setDisable(true); //启用
        //添加到数据库 - 添加后才会有自增id
        logininfoMapper.add(logininfo);
        //设置employee表的Logininfo_id字段
        employee.setLogininfo_id(logininfo.getId());
        super.add(employee);
        if (employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }
    }

    @Override
    @Transactional
    public void update(Employee employee) {
        employeeMapper.delRole(employee.getId());
        if (employee.getRole_id() != null){
            employeeMapper.addRole(employee);
        }
        //生成盐值，再加密加盐
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //获取到之前的数据 - 有一些数据前端没有修改，我们获取出来后重新添加进去
        Employee dbEmployee = employeeMapper.findOne(employee.getId());
        employee.setLogininfo_id(dbEmployee.getLogininfo_id());
        employee.setShop_id(dbEmployee.getShop_id());
        super.update(employee);
        //根据logininfo_id获取到数据库中的logininfoer
        Logininfo logininfo = logininfoMapper.findOne(employee.getLogininfo_id());
        //同步修改logininfo中的字段
        logininfo.setUsername(employee.getUsername());
        logininfo.setPhone(employee.getPhone());
        logininfo.setEmail(employee.getEmail());
        logininfo.setSalt(employee.getSalt());
        logininfo.setPassword(employee.getPassword());
        logininfoMapper.update(logininfo);
    }

    @Override
    @Transactional
    public void del(Long id) {
        employeeMapper.delRole(id);
        //根据id获取到employee
        Employee employee = employeeMapper.findOne(id);
        //根据Logininfo_id删除logininfo
        logininfoMapper.del(employee.getLogininfo_id());
        super.del(id);
    }

    @Override
    @Transactional
    public void patchDel(Long[] ids) {
        for (Long id : ids) {
            //根据id获取到employee
            Employee employee = employeeMapper.findOne(id);
            //根据Logininfo_id删除logininfo
            logininfoMapper.del(employee.getLogininfo_id());
            employeeMapper.del(id);
        }
    }

    @Override
    public List<Employee> list(Long shopId) {
        return employeeMapper.findByShopId(shopId);
    }
}